var myRoot = new Array();
myRoot[0] = "Type";
myRoot[1] = "Email";

function buildTree() {
	// create a new tree:
	tree = new YAHOO.widget.TreeView("treeContainer");

	// get root node for tree:
	var root = tree.getRoot();

	// create dummy data for root
	

	for (i = 0; i < myRoot.length; i++) {
		/*var tmpNode = new YAHOO.widget.TextNode( {
			label : mycars[i],
			customData : mycars[i] + "-metadata"
		}, root, false);*/
		var tmpNode = new YAHOO.widget.HTMLNode(myRoot[i], root, false);
		// set dynamic loading just for these top-level nodes
		tmpNode.setDynamicLoad(loadNodeData);
		
	}
	tree.draw();
}

function loadNodeData(node, fnLoadComplete) {	
	
	var sUrl,postData;
	var nodeId = node.contentElId;
	if(node.html == myRoot[0]){
		sUrl = ContextPath + "/ProjectList.do?action=getAllType";		
		postData = "";	
		
	}else if(node.html == myRoot[1]){
		sUrl = ContextPath + "/ProjectList.do?action=getAllEmail";		
		postData = "";
		
	}else{fnLoadComplete; return;}
	YAHOO.util.Connect.asyncRequest(
        'POST',
        sUrl,
        {
            success: function (o) {
        		var arrType = YAHOO.lang.JSON.parse(o.responseText);
        		var nodeObject = tree.getNodeByElement(document.getElementById(nodeId));
        		for ( var i = 0; i < arrType.length; i++) {            			
        			thisModel = "<A onClick=\"handleNodeClick(" + (nodeObject.depth + 1)+ "," + arrType[i][2] + ")\">" + arrType[i][0] + "(" + arrType[i][1] + ")" + "</A>";
        			var newNode = new YAHOO.widget.HTMLNode(thisModel, node, false);
        			//newNode.onLabelClick = onLabelClick;
        			newNode.setDynamicLoad(loadNodeData);
        		}
        		fnLoadComplete();
            },
            failure: function (o) {
                alert(o.statusText);
                fnLoadComplete();
            },
            
            //our handlers for the XHR response will need the same
            //argument information we got to loadNodeData, so
            //we'll pass those along:
            argument: {
                "node": node,
                "fnLoadComplete": fnLoadComplete
            }/*,
            
            //timeout -- if more than 7 seconds go by, we'll abort
            //the transaction and assume there are no children:
            timeout: 7000*/
        },
        postData
    );		
}

function handleNodeClick(depth,id){	
	if(depth == 1){
		window.su = "google.com";
	
	}
}
